package com.ocom.payconfig.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.payconfig.EntityPackageType;
import com.ocom.common.entity.payconfig.EntityPackageVoucher;
import com.ocom.common.request.callpackage.*;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.PackageTypeService;
import com.ocom.payconfig.service.PackageVoucherService;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
public class PackageTypeController {

    @Autowired
    private PackageTypeService packageTypeService;
    @Autowired
    private PackageVoucherService packageVoucherService;

    @PostMapping(PayConfigUrl.Url_getCallPackageConfig)
    public Result getCallPackageConfig(@RequestBody OnShowRequest request) {
        List<EntityPackageType> packageTypeList = packageTypeService.getPackageTypeList(request.getComId(), request);
        List<OnShowResponse> onShowResponses = JSON.parseArray(JSON.toJSONString(packageTypeList), OnShowResponse.class);
        if (0 < packageTypeList.size()) {
            List<OnShowPackageDetail> entityPackageVouchers = packageVoucherService.selectByComId(request.getComId());
            Map<Integer, List<OnShowPackageDetail>> listMap = entityPackageVouchers.stream().collect(Collectors.groupingBy(OnShowPackageDetail::getPackageTypeId));
            for (OnShowResponse onShowResponse : onShowResponses) {
                onShowResponse.setPackageDetails(listMap.get(onShowResponse.getPackageTypeId()));
            }
        }
        return ResultUtil.success(onShowResponses);
    }

    @PostMapping(PayConfigUrl.Url_addCallPackageConfig)
    public Result addCallPackageConfig(@RequestBody EntityPackageType request) {
        return ResultUtil.success(packageTypeService.addCallPackageConfig(request.getComId(), request));
    }

    @PostMapping(PayConfigUrl.Url_editCallPackageConfig)
    public Result editCallPackageConfig(@RequestBody EntityPackageType request) {
        return ResultUtil.success(packageTypeService.editCallPackageConfig(request.getComId(), request));
    }

    @PostMapping(PayConfigUrl.Url_deleteCallPackageConfig)
    public Result deleteCallPackageConfig(@RequestBody DeleteConfigRequest request) {
        return ResultUtil.success(packageTypeService.deleteCallPackageConfig(request.getComId(), request));
    }

    @PostMapping(PayConfigUrl.Url_saveCallPackageDetail)
    public Result saveCallPackageDetail(@RequestBody ModifyCallPackageDetailRequest request) {
        boolean result = packageVoucherService.deleteByPackageTypeId(request.getComId(), request.getPackageTypeId());
        List<EntityPackageVoucher> packageVouchers = JSON.parseArray(JSON.toJSONString(request.getPackageDetail()), EntityPackageVoucher.class);
        if (null != packageVouchers && 0 < packageVouchers.size()) {
            Date now = new Date();
            ListIterator<EntityPackageVoucher> iterator = packageVouchers.listIterator();
            while (iterator.hasNext()){
                EntityPackageVoucher entityPackageVoucher = iterator.next();
                entityPackageVoucher.setPackageTypeId(request.getPackageTypeId());
                entityPackageVoucher.setCreateUserid(request.getUserId());
                entityPackageVoucher.setComId(request.getComId());
                entityPackageVoucher.setCreateTime(now);
                entityPackageVoucher.setUpdateTime(now);
            }
            result = packageVoucherService.saveBatch(packageVouchers);
        }
        return ResultUtil.success(result);
    }

}
